<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="成果标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入成果标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <div>
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item
          :title="item.name"
          :name="item.id"
          v-for="item in planList"
          :key="item.id"
        >
          <div>
           <span class="plan-title"> 计划简介：</span>
            <div v-html="item.description"></div>
          </div>

          <div>
            <span class="plan-title">计划时间：</span>
            <div >{{item.startTime}}至{{item.endTime}}</div>
          </div>
           <div>
            <span class="plan-title">计划状态：</span>
            <div >{{item.status}}</div>
          </div>
          <div>
            <span class="plan-title">计划进度：</span>
            <div >{{item.progress}}</div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div> -->
    <el-table
      v-loading="loading"
      :data="planList"
      @selection-change="handleSelectionChange"
      :preserve-expanded-content="preserveExpanded"
    >
      <el-table-column type="expand">
        <template #default="tableProps">
          <div style="padding:0 50px">
            <div class="plan-title">任务描述：</div>
            <div v-html="tableProps.row.description" class="ql-editor"></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="noticeId" width="50">
        <template #default="scope">
          <span>{{
            scope.$index +
            1 +
            (queryParams.pageDomain.pageNum - 1) *
              queryParams.pageDomain.pageSize
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="任务名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="优先级"
        align="center"
        prop="priority"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="开始时间"
        align="center"
        prop="开始时间"
        width="200"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="结束时间"
        width="200"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="进度" align="center" prop="status" width="250">
        <template #default="scope">
          <el-progress :percentage="scope.row.progress" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
  <PreviewDialog v-model="dialogVisible" :infor="currentInfor"></PreviewDialog>
</template>

<script setup name="Information">
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/system/notice";
import { getPlanList } from "@/api/pms/project";
import PreviewDialog from "./previewDialog.vue";
const props = defineProps({
  projectId: {
    type: String,
  },
});
const route = useRoute();
const router = useRouter();

const preserveExpanded = ref(false);

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const planList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogVisible = ref(false);
const currentTaskId = ref("");
const currentInfor = ref({});
const activeName = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageDomain: {
      isAsc: "true",
      orderByColumn: "",
      pageNum: 1,
      pageSize: 10,
      reasonable: true,
    },
    id: "",
    name: "",
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  getPlanList(queryParams.value).then((response) => {
    planList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  // open.value = true;
  // title.value = "添加公告";
  currentTaskId.value = "";
  dialogVisible.value = true;
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  currentTaskId.value = row.id;
  dialogVisible.value = true;
}

/**跳转活动详情 */
function goDetail(row) {
  reset();
  // const noticeId = row.noticeId || ids.value;
  // getNotice(noticeId).then((response) => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改公告";
  // });
  router.push({ path: `detail/${row.id}` });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function submit(formData) {
  addTask({ ...formData, stageId: props.projectId }).then((response) => {
    proxy.$modal.msgSuccess("新增成功");
    getList();
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 下载文件 */
function download(item) {
  proxy.download(
    "/common/download",
    {
      fileName: item.url.replace("/profile/upload", ""),
      delete: false,
    },
    item.name
  );
}
//打开在线预览弹窗
function preview(item) {
  currentInfor.value = item;
  dialogVisible.value = true;
}
// onMounted(() => {
//   getList();
// });

watch(
  () => props.projectId,
  (newId, oldId) => {
    if (newId) {
      queryParams.value.id = props.projectId;
      getList();
    }
  },
  { deep: true, immediate: true }
);
</script>

<style scoped lang="scss">
.list-row {
  padding: 10px 0;
  margin: 10px 0;
  border-bottom: 1px dashed #cccccc;
  .title {
    padding: 10px 0;
  }
  .intro {
    color: #cccccc !important;
  }
}
.plan-title {
  color: #046ded;
  font-weight: bold;
  margin: 10px 0;
  display: inline-block;
}
</style>